package de.fhtrier.gdw2.ships.states;

import de.fhtrier.gdw2.ships.SpaceshipGame;
import de.fhtrier.gdw2.ships.game.Constants;
import de.fhtrier.gdw2.ships.game.World;
import de.fhtrier.gdw2.ships.game.entities.Unit;
import de.fhtrier.gdw2.ships.game.interfaces.ISelecteable;
import de.fhtrier.gdw2.ships.game.modules.Move;
import de.fhtrier.gdw2.ships.menu.MenuManager;
import de.fhtrier.gdw2.ships.network.datagrams.LevelDatagram;
import de.fhtrier.gdw2.ships.utils.AssetLoader;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * 
 */
public class GameplayState extends BasicGameState
	{

		public boolean go = false;

		// Default State ID Wert -1;
		int stateID = SpaceshipGame.GAMESTATE;
		World gameWorld;

		GameContainer container;
		StateBasedGame game;

		static boolean menuOpened;
		MenuManager menuManager;
		protected Image screen;
		
		/**
		 * Initialisierung
		 * 
		 * @param GameContainer
		 * @param StateBasedGame
		 */
		@Override
		public void init(GameContainer container, StateBasedGame game)
				throws SlickException
			{
				this.container = container;
				this.game = game;
				this.gameWorld = new World();
				menuManager = new MenuManager(container, game,
						MenuManager.Type.INGAME);

				// LALALALALA
				//	Test: Entity erstellen
				gameWorld.addEntity(new Unit(0, 12 * Constants.COMPONENTSIZE,
						8 * Constants.COMPONENTSIZE, AssetLoader.getInstance()
								.getImage("rund")));

				//Test: Module hizufügen
				gameWorld.getEntity(0).addModule(
						new Move(0, 0, 0, AssetLoader.getInstance().getImage(
								"mod_central")));
				gameWorld
						.getEntities()
						.get(0)
						.addModule(
								new Move(0, 0, 1, AssetLoader.getInstance()
										.getImage("mod_antrieb_C")));
				gameWorld
						.getEntities()
						.get(0)
						.addModule(
								new Move(0, 1, 0, AssetLoader.getInstance()
										.getImage("mod_weap_B")));
				gameWorld
						.getEntities()
						.get(0)
						.addModule(
								new Move(0, 0, -1, AssetLoader.getInstance()
										.getImage("mod_weap_A")));
				gameWorld
						.getEntities()
						.get(0)
						.addModule(
								new Move(0, -1, 0, AssetLoader.getInstance()
										.getImage("mod_weap_D")));

				gameWorld.getEntities().get(0).setTypeName("Spaceship ONE");
				
				container.getInput().enableKeyRepeat();
				screen = new Image("res/menu/screen.png");
			}

		@Override
		/**
		 * @param GameContainer
		 * @param StatedBasedGame
		 * @param Graphics
		 */
		public void render(GameContainer container, StateBasedGame game,
				Graphics g) throws SlickException
			{
				g.setBackground(Color.black);
				g.setColor(Color.white);


				
				this.gameWorld.render(container, game, g);

				if (menuOpened)
					menuManager.render(container, game, g);

			}

		@Override
		/**
		 * @param GameContainer
		 * @param StateBasedGame
		 * @param int
		 */
		public void update(GameContainer container, StateBasedGame game,
				int delta) throws SlickException
			{

				this.gameWorld.update(container, game, delta);
				if (SpaceshipGame.isClient() || SpaceshipGame.isServer())
					{
						SpaceshipGame.send(this.gameWorld.getDatagram());

					}
			}

		public void receiveLevelDatagram(LevelDatagram datagram)
			{
				if (SpaceshipGame.isClient() || SpaceshipGame.isServer())
					{
						this.gameWorld.reveiveDatagram(datagram);
					}
			}

		@Override
		/**
		 * @return state ID
		 */
		public int getID()
			{
				// TODO Auto-generated method stub
				return stateID;
			}

		public void keyReleased(int key, char c)
			{
				if (menuOpened)
					{
						menuOpened = menuManager.keyReleased(key, c);
					} else if (key == Input.KEY_ESCAPE)
					{
						menuOpened = true;
					} else
						{
							this.gameWorld.keyReleased(key, c);
						}
			}

		/**
		 * @see org.newdawn.slick.InputListener#keyPressed(int, char)
		 */
		@Override
		public void keyPressed(int key, char c)
			{
				if (menuOpened)
					menuManager.keyPressed(key, c);
				else
					{
						this.gameWorld.keyPressed(key, c);

					}
			}

		public void mouseMoved(int oldx, int oldy, int newx, int newy)
			{
				if (menuOpened)
					{
						menuManager.mouseMoved(oldx, oldy, newx, newy);
					} else
					{
						this.gameWorld.mouseMoved(oldx, oldy, newx, newy);
					}
			}

		public void mouseDragged(int oldx, int oldy, int newx, int newy)
			{
				if (menuOpened)
					{
						menuManager.mouseDragged(oldx, oldy, newx, newy);
					} else
					{
						this.gameWorld.mouseDragged(oldx, oldy, newx, newy);
					}
			}

		public void mouseReleased(int button, int x, int y)
			{
				if (menuOpened)
					{
						menuManager.mouseReleased(button, x, y);
					} else
					{
						this.gameWorld.mouseReleased(button, x, y);
					}
			}

		public void mousePressed(int button, int x, int y)
			{
				if (menuOpened)
					{
						menuManager.mousePressed(button, x, y);
					} else
					{
						this.gameWorld.mousePressed(button, x, y);
					}
			}

		public void mouseWheelMoved(int newValue)
			{
				if (menuOpened)
					menuManager.mouseWheelMoved(newValue);
			}

		public void mouseClicked(int button, int x, int y, int clickCount)
			{
				this.gameWorld.mouseClicked(button, x, y, clickCount);// TODO
			}

		@Override
		public void enter(GameContainer container, StateBasedGame game)
				throws SlickException
			{
				menuOpened = false;
				menuManager.activate();

			}

		@Override
		public void leave(GameContainer container, StateBasedGame game)
				throws SlickException
			{
				menuOpened = false;
				menuManager.activate();
				this.gameWorld = new World();
			}

		public static void hideMenu()
			{
				menuOpened = false;
			}

		public World getGameWorld()
			{
				return this.gameWorld;
			}

	}
